package pdfsetter.gui;

import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComponent;

import pdfsetter.core.pages.PageRectangle;
import pdfsetter.core.schema.FillingSchema;
import pdfsetter.core.schema.FixedFillingSchema;
import pdfsetter.core.schema.PageSelection;
import pdfsetter.core.schema.SinglePageSchema;

@SuppressWarnings("serial")
public class PDFSchemaComponent extends JComponent {
	
	private SinglePageSchema schema;
	private double width, height;
	private int width0, height0;
	private MouseListener mouseListener = null;
	private PDFListPane pdfLlst;
	private List<String> desc;
	private List<PageSelection> pageSelections;
	
	public PDFSchemaComponent(SinglePageSchema schema, PDFListPane pdfList) {
		super();
		this.schema = schema;
		this.pdfLlst = pdfList;
		if(schema == null) return;
		desc = new ArrayList<String>(schema.getPageLocations().size());
		pageSelections = new ArrayList<PageSelection>(schema.getPageLocations().size());
		for(int i=0;i<schema.getPageLocations().size();++i)
		{
			desc.add("-none-");
			pageSelections.add(null);
		}
		width = schema.getPageInfo().getWidth();
		height = schema.getPageInfo().getHeight();
	}
	
	private void drawString(Graphics g, String text, int x, int y) {
        for (String line : text.split("\n"))
            g.drawString(line, x, y += g.getFontMetrics().getHeight());
    }
	
	@Override
	public void paint(final Graphics g) {
		if(schema == null) return;
		width0 = (int) (getSize().getWidth()-width)/2;
		height0 = (int) (getSize().getHeight()-height)/2; 
		
		g.drawRect(width0, height0, (int) width, (int) height);
		
		int ind = 0;
		for(PageRectangle rect : schema.getPageLocations())
		{
			g.drawRect(width0 + (int) rect.getHorizontalShift(), height0 + (int) rect.getVerticalShift(), (int) rect.getWidth(), (int) rect.getHeight());
			drawString(g, desc.get(ind), width0 + (int) rect.getHorizontalShift() + (int) rect.getWidth() / 3, height0 + (int) rect.getVerticalShift() + (int) rect.getHeight() / 3);
			++ind;
		}
		if(mouseListener != null)
			removeMouseListener(mouseListener);
		mouseListener = new MouseAdapter() {
			
			@Override
			public void mouseClicked(MouseEvent e) {
				int x = e.getX();
				int y = e.getY();
				int ind = 0;
				for(PageRectangle rect : schema.getPageLocations())
				{
					if(width0 + (int) rect.getHorizontalShift() < x &&
							x < width0 + (int) rect.getHorizontalShift() + rect.getWidth() &&
							height0 + (int) rect.getVerticalShift() < y &&
							y < height0 + (int) rect.getVerticalShift() + rect.getWidth()) {
						desc.set(ind, pdfLlst.getPageSelectionDesc().replaceAll("(.{10})", "$1\n"));
						pageSelections.set(ind, pdfLlst.getPageSelection());
						break;
					}
					++ind;
				}
				repaint();
			}
		};
		addMouseListener(mouseListener);
	}
	
	public FillingSchema getFillingSchema() {
		return new FixedFillingSchema(pageSelections);
	}
}
